<?php
namespace haibao\cms\business\control\pc\channel\pic;

use haibao\frame\data\query\Condition;
use \haibao\cms\model\filter\imagecollective\ImageCollective as ImageCollectiveFilter;

class BannerImagesController extends \haibao\cms\business\control\pc\channel\ChannelController{
	private static $banners = array(
			310=>array(
					'id'=>310,
					'name'=>'时装趋势',
					'children'=>array(
							'340'=>'街拍',
							'378'=>'搭配',
							'311'=>'秀场趋势',
							'8434'=>'时尚圈私照',
							'2371'=>'时尚杂志',
					)
			),
			24=>array(
					'id'=>24,
					'name'=>'美容趋势',
					'children'=>array(
							'8445'=>'秀场Beauty',
							'1376'=>'明星Beauty',
							'11012'=>'街拍Beauty',
							'130'=>'彩妆',
							'146'=>'发型',
							'25'=>'美容护肤',
							'11013'=>'美妆大片',
							'252'=>'美体',
					)
			),
			955=>array(
					'id'=>955,
					'name'=>'时尚品牌',
					'children'=>array(
							'324'=>'时装秀场',
							'2848'=>'设计师',
							'2826'=>'广告大片',
							'5998'=>'lookbook',
							'8192'=>'品牌活动',
					)
			),
			6863=>array(
					'id'=>6863,
					'name'=>'珠宝腕表',
					'children'=>array(
							'8992'=>'珠宝腕表街拍',
							'11001'=>'珠宝腕表明星示范',
							'8239'=>'珠宝腕表搭配',
							'8302'=>'珠宝腕表大片',
					)
			),
			7455=>array(
					'id'=>7455,
					'name'=>'配饰',
					'children'=>array(
							'7959'=>'配饰街拍',
							'7768'=>'配饰明星示范',
							'11009'=>'配饰搭配',
							'8323'=>'配饰工艺细节',
							'11014'=>'配饰秀场细节',
							'11015'=>'配饰大片',
					)
			),
			1383=>array(
					'id'=>1383,
					'name'=>'明星',
					'children'=>array(
							'2272'=>'明星街拍',
							'2942'=>'明星子女',
							'3843'=>'明星私照',
							'370'=>'明星红毯',
							'2265'=>'影视剧照',
							'8234'=>'明星杂志大片'
					)
			),
			520=>array(
					'id'=>520,
					'name'=>'生活',
					'children'=>array(
							'23,3544'=>'风光·摄影',
							'519,3067'=>'家居·设计',
							'521'=>'缤纷美食',
							'5400'=>'城市生活',
							'668'=>'健康健身',
							'4677'=>'宠物趣味',
							'1513'=>'Sex&Love',
							'11017'=>'美物美图',
					)
			),
			211=>array(
					'id'=>211,
					'name'=>'新娘',
					'children'=>array(
							'212'=>'婚纱',
							'11016'=>'新娘美妆',
							'5263'=>'明星婚礼',
							'228'=>'婚礼',
							'219'=>'结婚礼服',
							'6632'=>'新娘捧花',
							'3631'=>'结婚蛋糕',
					)
			),
			\haibao\cms\business\control\AmericanController::AMERICAN_TOP_TAG_ID=>array(
					'id'=>\haibao\cms\business\control\AmericanController::AMERICAN_TOP_TAG_ID,
					'name'=>'美剧',
					'children'=>array(
							'11281'=>'美剧追踪',
							'11282'=>'剧评影评',
							'11283'=>'片花视频',
							'11284'=>'红毯颁奖',
							'11317'=>'美剧库',
					)
			),
	);
	
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	public function displayTemplateHtml(){
		$isAmerican = false;
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$data = array();
		$bannerId = $controlData['manual']['bannerId'];
		$ids = explode(',',$bannerId);
		$banners = self::$banners;
		$isParent = false;
		if (isset($banners[$bannerId])){
			$isParent = true;
			$childBanners = $banners[$bannerId]['children'];
			foreach ($childBanners as $key=>$value){
				$ids = array_merge($ids,explode(',',$key));
			}
		}
		
		if ($bannerId == \haibao\cms\business\control\AmericanController::AMERICAN_TOP_TAG_ID){
			$isAmerican = true;	
		}
		
		$pager = new \haibao\frame\data\query\Pager(6,1);
		$result = \haibao\cms\business\SphinxDb::searchImageList($ids,$isParent,$pager);
		
		if ($result){
			$tagIds = array();
			$domainName = 'www';
			$topTags = self::getTopTags();
			if(isset($topTags[$bannerId])){
				$domainName = trim($topTags[$bannerId]['domain']);
			}
			$imageIds = $imageData = $articleIds = $articleData = $unionData =array();
			foreach ($result as $value){
				if ($isAmerican){
					array_push($tagIds,$value->PrimaryTagId);
				}
				
				if ($value->Type == \haibao\cms\model\data\SphinxDb::TYPE_ARTICLE){
					array_push($articleIds,$value->RecordId);
				}else if ($value->Type == \haibao\cms\model\data\SphinxDb::TYPE_IMAGE){
					array_push($imageIds,$value->RecordId);
				}
			}
			
			$tagIds = array_unique($tagIds);
			if ($tagIds){
				$tagIds = \haibao\cms\business\Tag::getTagsById($tagIds);
			}
			
			if ($imageIds){
				$imageData = $this->getImageData($imageIds,$bannerId);
			}
			if ($articleIds){
				$articleData = $this->getArticleData($articleIds);
			}
			
			if ($imageData || $articleData){
				foreach ($result as $value){
					if ($value->Type == \haibao\cms\model\data\SphinxDb::TYPE_ARTICLE){
						foreach ($articleData as $article){
							if ($article['Id'] == $value->RecordId){
								$article['AccessUrl'] = '/'.$domainName.'/article/'.$article['PublishId'].'.htm';
								$article['Type'] = \haibao\cms\model\data\SphinxDb::TYPE_ARTICLE;
								$article['TagUrl'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/tv/'.$value->PrimaryTagId.'/';
								
								if ($isAmerican && isset($tagIds[$value->PrimaryTagId])){
									$article['TagName'] = $tagIds[$value->PrimaryTagId]['TagName'];								
								}
								
								array_push($unionData,$article);
							}
						}
					}else if ($value->Type == \haibao\cms\model\data\SphinxDb::TYPE_IMAGE){
						foreach ($imageData as $img){
							if ($img['Id'] == $value->RecordId){
								$img['AccessUrl'] = '/'.$domainName.'/pics/'.$img['Id'].'.htm';
								$img['Type'] = \haibao\cms\model\data\SphinxDb::TYPE_IMAGE;
								$img['TagUrl'] = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL).'/tv/'.$value->PrimaryTagId.'/';
								
								if ($isAmerican && isset($tagIds[$value->PrimaryTagId])){
									$img['TagName'] = $tagIds[$value->PrimaryTagId]['TagName'];
								}
								
								array_push($unionData,$img);
							}
						}
					}
				}
			}

			$data['data'] = $unionData;
			$data['bannerName'] = self::$banners[$bannerId]['name'];
			$data['domainName'] = $domainName;
			$data['bannerImage'] = $this->getBannerImage($bannerId);
			$data['bannerClass'] = $this->getBannerClass($bannerId);
		}
		
		$path = $this->generateHtmlPath(__CLASS__);
		$isNewHtml = $this->generateHtml($path,$content);
		$this->assign('data',$data);
		$this->assign('bannerId',$bannerId);
		//try{
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content,true);
		//}
		//catch (\Exception $e){
		//	echo $e;
		//}
		return $content;
	}
	
	private function getArticleData($articleIds){
		$articleFilter = new \haibao\cms\model\filter\Article();
		$articleFilter->select(array(
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_TITLE,
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,
		));
		$articleFilter->setPager(new \haibao\frame\data\query\Pager(6,1));
		$articleFilter->order(\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_PUBLISHTIME,false);
		$articleFilter->where(
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID,
				Condition::CONDITION_IN,
				$articleIds
		);
		$proFilter = new \haibao\cms\model\filter\ArticleProImage();
		$proFilter->select(array(
				\haibao\cms\model\filter\ArticleProImage::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\ArticleProImage::CONDITION_FIELD_NAME_PIC_TYPE,
				\haibao\cms\model\filter\ArticleProImage::CONDITION_FIELD_NAME_PIC_URL,
				\haibao\cms\model\filter\ArticleProImage::CONDITION_FIELD_NAME_WIDTH,
				\haibao\cms\model\filter\ArticleProImage::CONDITION_FIELD_NAME_HEIGHT
		));
		$proFilter->where(
				\haibao\cms\model\filter\ArticleProImage::CONDITION_FIELD_NAME_PIC_TYPE,
				Condition::CONDITION_EQUAL,
				\haibao\cms\model\data\ArticleProImage::PIC_TYPE_INTRO	
		);
		$articleFilter->innerJoin(
				$proFilter,
				null,
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\ArticleProImage::CONDITION_FIELD_NAME_ARTICLE_ID,
				\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_PRO_IMAGE,
				true
		);
		
		$articles = \haibao\cms\business\Article::getArticleList($articleFilter);
		
		if ($articles){
			$data = array();
			foreach ($articles as $article){
				$url = '';
				foreach ($article->ArticleProImage as $pro){
					$url = $pro->PicUrl;
					if ($pro->Width == $pro->Height){
						break;
					}
				}
				
				$article->ArticleProImage = \haibao\cms\business\control\base::getImageLink($url,240,240,90);
				
				$arr = $article->toArray(true,true);
				$arr['ProImage'] = $arr['ArticleProImage'];
				array_push($data,$arr);
			}
			
			$articles = $data;
		}
		
		return $articles;
	}
	
	private function getImageData($imageIds,$bannerId){
		$imageFilter = new \haibao\cms\model\filter\ImageCollective\ImageCollective();
		$imageFilter->select(array(
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_TITLE,
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_SHORT_TITLE,
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_DESCRIPTION,
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_CREATE_TIME,
		));
		$imageFilter->setPager(new \haibao\frame\data\query\Pager(6,1));
		$imageFilter->order(\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,false);
		$imageFilter->where(
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_ID,
				Condition::CONDITION_IN,
				$imageIds		
		);
		
		$proFilter = new \haibao\cms\model\filter\imagecollective\ImageCollectivePro();
		$proFilter->select(array(
				\haibao\cms\model\filter\ImageCollective\ImageCollectivePro::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\ImageCollective\ImageCollectivePro::CONDITION_FIELD_NAME_IMAGE_ID,
				\haibao\cms\model\filter\ImageCollective\ImageCollectivePro::CONDITION_FIELD_NAME_IMAGE_DESCRIPTION,
				\haibao\cms\model\filter\ImageCollective\ImageCollectivePro::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE
		));
		
		$libraryFilter = new \haibao\cms\model\filter\ImageLibrary();
		$libraryFilter->select(
				\haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_FILENAME
		);
		
		$imageFilter->innerJoin(
				$proFilter,
				null,
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\ImageCollective\ImageCollectivePro::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
				\haibao\cms\model\filter\ImageCollective\ImageCollective::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PRO,
				true
		);
		$imageFilter->innerJoin(
				$libraryFilter,
				$proFilter,
				\haibao\cms\model\filter\ImageCollective\ImageCollectivePro::CONDITION_FIELD_NAME_IMAGE_ID,
				\haibao\cms\model\filter\ImageLibrary::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\ImageCollective\ImageCollectivePro::CONDITION_FIELD_NAME_IMAGE_FILENAME,
				false
		);
		
		$images = \haibao\cms\business\imagecollective\ImageCollective::getImageCollectiveByFilter($imageFilter);
		
		if ($images){
			$data = array();
			foreach ($images as $image){
				$targetImageUrl = '';
				foreach ($image->ImageCollectivePro as $pro){
					$targetImageUrl = $pro->FileName->Filename;
					if ($pro->ImageType == \haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_FOUR){
						break;
					}
				}
				if($bannerId == \haibao\cms\business\control\AmericanController::AMERICAN_TOP_TAG_ID){
					$image->ImageCollectivePro = \haibao\cms\business\control\base::getImageLink($targetImageUrl,240,180);
				}else{
					$image->ImageCollectivePro = \haibao\cms\business\control\base::getImageLink($targetImageUrl,240,240);
				}
				
				$arr = $image->toArray(true,true);
				$arr['ProImage'] = $arr['ImageCollectivePro'];
				array_push($data,$arr);
			}
			$images = $data;
		}
		
		return $images;
	}
	
	private function getBannerImage($bannerId){
		$image = '';
		$domainHost = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_STATIC_DOMAIN);
		if(\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_CDN_DEFAULT)){
			$domainHost = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_CDN_DOMAIN);
		}
		switch ($bannerId){
			case 310:
				$image = $domainHost.'/images/pictures_fl00.jpg';
				break;
			case 24:
				$image = $domainHost.'/images/pictures_fl01.jpg';
				break;
			case 955:
				$image = $domainHost.'/images/pictures_fl02.jpg';
				break;
			case 6863:
				$image = $domainHost.'/images/pictures_fl03.jpg';
				break;
			case 7455:
				$image = $domainHost.'/images/pictures_fl04.jpg';
				break;
			case 1383:
				$image = $domainHost.'/images/pictures_fl05.jpg';
				break;
			case 520:
				$image = $domainHost.'/images/pictures_fl06.jpg';
				break;
			case 211:
				$image = $domainHost.'/images/pictures_fl07.jpg';
				break;

			case \haibao\cms\business\control\AmericanController::AMERICAN_TOP_TAG_ID:
				$image = $domainHost.'/images/pictures_fl09.jpg';
				break;
		}
	
		return $image;
	}
	
	private function getBannerClass($bannerId){
		$class = '';
	
		switch ($bannerId){
			case 310:
				$class = 'alink01';
				break;
			case 24:
				$class = 'alink02';
				break;
			case 955:
				$class = 'alink03';
				break;
			case 6863:
				$class = 'alink04';
				break;
			case 7455:
				$class = 'alink05';
				break;
			case 1383:
				$class = 'alink06';
				break;
			case 520:
				$class = 'alink07';
				break;
			case 211:
				$class = 'alink08';
				break;
			case \haibao\cms\business\control\AmericanController::AMERICAN_TOP_TAG_ID:
				$class = 'alink09';
				break;
		}
	
		return $class;
	}
}